﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AdminApi.Types;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OtherLib.TenXun;

namespace AdminApi.Controllers
{
    [ApiController]
    public class FileController : ControllerBase
    {
        [HttpPost,Route("api/File/UploadFiles")]
        public ActionResult UploadFiles(UploadFileType uploadFileType)
        {
            Dictionary<string, object> payLoad = (Dictionary<string, object>)this.HttpContext.Items["payLoad"];

            var adminId = payLoad!=null?payLoad["adminId"]:"1";

            List<string> list = new List<string>();
            foreach (var formFile in Request.Form.Files)
            {
                byte[] bytes;
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    formFile.CopyTo(memoryStream);
                    bytes = memoryStream.ToArray();
                }

                var path = CosUtil.UploadObject($"admin/admin-{adminId}/{uploadFileType.ToString()}/{Guid.NewGuid().ToString("N")}{Path.GetExtension(formFile.FileName)}", bytes);

                list.Add(path);
            }            

            return this.Success(list);
        }
    }
}
